package ro.prjmanager.web.actions.tableset;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import ro.prjmanager.core.usr.UserSession;
import ro.prjmanager.model.*;
import ro.prjmanager.service.SynchInterfaceService;
import ro.prjmanager.web.actions.GeneralAction;
import ro.prjmanager.web.annotation.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static ro.prjmanager.service.SynchInterfaceService.MYSQL;
import static ro.prjmanager.service.SynchInterfaceService.ORACLE;


@Service(target = "synchService")
@SuppressWarnings("unchecked")
public class TableSetAction extends GeneralAction {

    private List<Map<String, String>> tableList;
    private List<String> tableIds;
    private boolean synchronizeAllTableSet;

    public List<String> getTableIds() {
        return tableIds;
    }

    public void setTableIds(List<String> tableIds) {
        this.tableIds = tableIds;
    }

    public List<Map<String, String>> getTableList() {
        return tableList;
    }

    public void setTableList(List<Map<String, String>> tableList) {
        this.tableList = tableList;
    }

    public boolean isSynchronizeAllTableSet() {
        return synchronizeAllTableSet;
    }

    public void setSynchronizeAllTableSet(boolean synchronizeAllTableSet) {
        this.synchronizeAllTableSet = synchronizeAllTableSet;
    }

    @DefaultHandler
    @HandlesEvent("showLoadTable")
    public Resolution loadForActiveSet() {
        SynchInterfaceService synchService = (SynchInterfaceService) getService();
        UserSession<Users, Projects, Svn, DataBases,Schema> userSession = getContext().getUserSession();


        List<TableSet> tableSetList = getService("tablesetService").list();
        if (tableSetList != null && !tableSetList.isEmpty()) {
            setTableIds(new ArrayList<String>());
            for (TableSet tableSet : tableSetList) {
                getTableIds().add(tableSet.getTableName().toUpperCase());
            }
        }
           int type = (userSession.getDataBases().getFkDbType().getName().
                equalsIgnoreCase("oracle")) ? ORACLE : MYSQL;
        String url = type==MYSQL?userSession.getDataBases().getUrl() + userSession.getSchema().getName():userSession.getDataBases().getUrl();
        List<String> list = synchService.load(url,
                userSession.getSchema().getUser(),
                userSession.getSchema().getPassword(),
                userSession.getSchema().getUser()
                , type);
        if (list != null) {
            setTableList(new ArrayList<Map<String, String>>());
            for (String str : list) {
                Map<String, String> table = new HashMap<String, String>();
                table.put("id", str.toUpperCase());
                table.put("name", str.toUpperCase());
                getTableList().add(table);
            }

        }
        return new ForwardResolution("/pages/nomenclators/synch/loadTableSet.jsp");
    }


    @HandlesEvent("persistTableSet")
    public Resolution saveActiveTables() {
        SynchInterfaceService synchService = (SynchInterfaceService) getService();
        UserSession<Users,Projects,Svn,DataBases,Schema> userSession = getContext().getUserSession();

        int type = (userSession.getDataBases().getFkDbType().getName().
                equalsIgnoreCase("oracle")) ? ORACLE : MYSQL;
        List<TableSet> tableSetList = getService("tablesetService").list();
        if (tableSetList != null && !tableSetList.isEmpty() && tableIds != null && !tableIds.isEmpty()) {
            for (String tableNew : tableIds) {
                boolean contain = false;
                for (TableSet tableSet : tableSetList) {
                    if (tableSet.getTableName().equalsIgnoreCase(tableNew)) {
                        contain = true;
                        break;
                    }
                }
                if (!contain) {
                    TableSet tableSet = new TableSet();
                    tableSet.setTableName(tableNew);
                    getService("tablesetService").create(tableSet);
                }
            }
            for (TableSet tableSet : tableSetList) {
                boolean contain = false;
                for (String tableNew : tableIds) {
                    if (tableSet.getTableName().equalsIgnoreCase(tableNew)) {
                        contain = true;
                        break;
                    }
                }
                if (!contain) {
                    String url = type==MYSQL?userSession.getDataBases().getUrl() + userSession.getSchema().getName():userSession.getDataBases().getUrl();
                    synchService.removeTableSet(
                            url,
                            userSession.getSchema().getUser(),
                            userSession.getSchema().getPassword(),
                            userSession.getSchema().getName()
                            , type, tableSet
                    );
                }
            }


        }else{
            ro.prjmanager.core.service.Service tableSetService=getService("tablesetService");
            if(tableIds!=null&&tableIds.size()>0)
            for(String tn:tableIds){
                TableSet ts=new TableSet();
                ts.setTableName(tn);
                tableSetService.create(ts);
            }
        }
        return loadForActiveSet() ;
    }

}
